//
//  IOTCSetTaskStartAndEndDateViewController.m
//  Tasks-Test3
//
//  Created by User-16 on 3/19/14.
//  Copyright (c) 2014 IOTrainingCamp. All rights reserved.
//

#import "IOTCSetTaskStartAndEndDateViewController.h"
#import "IOTCAddToGoogleCalendarsViewController.h"
#import "IOTCTask.h"

@interface IOTCSetTaskStartAndEndDateViewController ()

@property (retain) IOTCTask* task;
@property (retain, nonatomic) IBOutlet UIDatePicker *startDatePicker;
@property (retain, nonatomic) IBOutlet UIDatePicker *endDatePicker;

@property (weak, nonatomic) IBOutlet UIButton *addToGoogleButton;
@property (weak, nonatomic) IBOutlet UIButton *addLocallyButton;


@end

@implementation IOTCSetTaskStartAndEndDateViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id) initWithTask:(IOTCTask*) task
{
    self = [self initWithNibName:nil bundle:nil];
    if(self)
    {
        self.task = task;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Task Time";
        // Do any additional setup after loading the view from its nib.
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)okAddTaskTimeLimits:(id)sender {
    
    if(![self areTimeLimitsCorrect])
    {
        [self timeLimitsIncorrectMessage];
        return;
    }
    
    if([self hasEndDatePassed])
    {
        [self endDateHasPassedMessage];
        return;
    }
    self.task.startDate = self.startDatePicker.date;
    self.task.endDate = self.endDatePicker.date;
    
    [self.task setLocalNotification];

    if(sender == self.addLocallyButton)
        [self.navigationController popToRootViewControllerAnimated:YES];
    
    if(sender == self.addToGoogleButton)
    {
        IOTCAddToGoogleCalendarsViewController* vc =[[IOTCAddToGoogleCalendarsViewController alloc] initWithNibName:nil bundle:nil];
        vc.task = self.task;
        [self.navigationController pushViewController:vc animated:YES];
    }


}

- (IBAction)cancelTaskTimeLimits:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(BOOL) areTimeLimitsCorrect
{
    if([self.endDatePicker.date timeIntervalSinceDate:self.startDatePicker.date] < 0) return NO;
    else return YES;
}

-(void) timeLimitsIncorrectMessage
{

        UIAlertView* incorrectLimitsAlert = [[UIAlertView alloc] initWithTitle:@"Incorrect start or end time" message:@"The end date is before the start date" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [incorrectLimitsAlert show];

}

-(BOOL) hasEndDatePassed
{
    if([self.endDatePicker.date timeIntervalSinceNow] <= 0) return YES;
    return NO;
    
}

-(void) endDateHasPassedMessage
{
    
        UIAlertView* endTimePassedAlert = [[UIAlertView alloc] initWithTitle:@"Incorrect end time" message:@"The end date for the task has passed" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [endTimePassedAlert show];
}




@end
